import leancloud
import os
import datetime
import json
leancloud.init("FuEKBAKt7fr6CnVTfMVXKyMm-gzGzoHsz", "9Gw1lGPn1fgxJ3gKw04BltIU")
raw_data = leancloud.Object.extend('RawData')
for file in os.listdir("../dataset/to_upload"):
    with open("../dataset/to_upload/" + file) as f:
        date = datetime.datetime(2020,
                                 int(file[:-4][0:2]),
                                 int(file[:-4][2:4]),
                                 int(file[:-4][4:6]),
                                 int(file[:-4][6:8]),
                                 int(file[:-4][8:10]))
        query = raw_data.query                                                  #RawData对象先查找若没有则创建，有则更新
        query.equal_to('event_name', '2019-nCoV')
        query.equal_to('time', date)
        raw_data_list = query.find()
        if raw_data_list == []:
            rd = raw_data()
            rd.set('event_name', '2019-nCoV')
            rd.set('time', date)
        else: 
            rd = raw_data_list[0]
        rd.set('data', json.dumps(json.loads(f.read())))
        rd.save()